﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace FrameDisplayServer.Browser
{
    public partial class ImageDisplayForm : Form
    {
        private Image image;

        public ImageDisplayForm()
        {
            InitializeComponent();
        }

        public void ShowImage(Image newImage)
        {
            if (this.InvokeRequired)
            {
                this.BeginInvoke(new Action<Image>(this.ShowImage), new object[] { newImage });
                return;
            }

            this.pictureBox1.Image = newImage;

            if (this.image != null)
            {
                this.image.Dispose();                
            }

            this.image = newImage;


            this.WindowState = FormWindowState.Normal;

            Screen screen = this.FindTargetScreen();
            this.Bounds = screen.Bounds;

            this.BringToFront();
        }       

        private Screen FindTargetScreen()
        {
            string targetScreen = AppCentral.SettingsManager.BrowserTargetScreenDeviceName;
            if (String.IsNullOrEmpty(targetScreen))
            {
                return Screen.PrimaryScreen;
            }

            Screen[] screens = Screen.AllScreens;
            foreach (Screen screen in screens)
            {
                if (screen.DeviceName == targetScreen)
                {
                    return screen;
                }
            }

            return Screen.PrimaryScreen;
        }
    }
}
